package coroutines.coroutine.cancel

import kotlinx.coroutines.*

class CanNotCancelTest {
}

fun main() {
    runBlocking {
        val startTime = System.currentTimeMillis()

        val job = launch(Dispatchers.Default) {
            var nextTime = startTime
            var i = 0

            while (i < 15) {
                if (isActive) {
                    if (System.currentTimeMillis() >= nextTime) {
                        println("job: I'm sleeping ${i++}")
                        nextTime += 500
                    }
                } else {
                    return@launch
                }
            }
        }

        delay(3000)

        println("main: I'm tried of waiting")
        job.cancelAndJoin()
        println("main: Now I can  quit.")
    }
}